package com.ykw.parking.controller;

import com.ykw.parking.mapper.OrderMapper;
import com.ykw.parking.mapper.ShoppingCartMapper;
import com.ykw.parking.mapper.UserMapper;
import com.ykw.parking.pojo1.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ShoppingCartMapper cartMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping("/order")
    public String order(HttpSession session){

        return "order";
    }

    @RequestMapping("/getOrder")
    @ResponseBody
    public List<UserOrder> getOrder(HttpSession session){
        String  username = (String)session.getAttribute("username");
        List parkings=new ArrayList();
        User user = userMapper.queryUserbyname(username);
        List<UserOrder> order = orderMapper.getOrder(user.getId());
        return order;
    }


    @RequestMapping("/delOrder")
    @ResponseBody
    public void delOrder(@RequestParam("deleteCartList[]") List<String> deleteCartList, HttpSession session){
        int i = orderMapper.delOrder(deleteCartList.get(0));
    }

    @RequestMapping("/updateOrder")
    @ResponseBody
    public String updateOrder(@RequestParam("deleteCartList[]") List<String> deleteCartList, HttpSession session){
        Double orderPrice = orderMapper.getOrderPrice(deleteCartList.get(0));
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        RealUser realUser = orderMapper.queryUser(user.getId());
        double money = userMapper.getMoney(user.getId());
        System.out.println(orderPrice);
        if(orderPrice>money){
            return "1";
        }
        UserOrder order = orderMapper.getOrderByOid(deleteCartList.get(0));
        ParkingLot parkingLot = orderMapper.getParkingLot(order.getPlid());
        Parking parking = orderMapper.getParking(Integer.valueOf(parkingLot.getPid()));
        orderMapper.updateParkingLot(1,parkingLot.getId());
        orderMapper.updateOrderAndOne(order.getOid(),parkingLot.getId());
        userMapper.addMoney(money-orderPrice,user.getId());
        orderMapper.addUserPaking(new UserParking(user.getId(),parking.getId(),parkingLot.getId(),parking.getCity(),
                parking.getAddress(),parkingLot.getNumber(),realUser.getName(),realUser.getId_number(),realUser.getPhone_number()));
        cartMapper.deleteCartByPId(String.valueOf(parkingLot.getId()));
        SimpleDateFormat sdf=new SimpleDateFormat("MM-dd");
        java.util.Date date=new java.util.Date();
        String str=sdf.format(date);
        redisTemplate.opsForValue().increment(str);
        return "0";
    }
}
